//获得手机号并验证（手机号等于十一位，都是数字）
document.querySelector(".telephone").onblur=function (){
    checkTelephone();
}

document.querySelector(".password").onblur=function (){
    checkPassword();
}

document.querySelector(".repassword").onblur=function (){
    checkRepassword();
}

document.querySelector(".username").onblur = function(){
    checkUsername();
}

function checkTelephone(){
    var telephone =document.querySelector(".telephone").value;
    console.log("telephone:"+telephone);
    if(telephone.trim()==""){
        document.querySelector(".telephone").placeholder="请输入手机号";
        return false;
    }else if(!/^[1][3,4,5,7,8][0-9]{9}$/.test(telephone)){
        document.querySelector(".telephone").value=null;
        document.querySelector(".telephone").placeholder="请输入正确的手机号";
        return false;
    }
    return true;
}


function checkPassword(){
    var userpwd = document.querySelector(".password").value;
    console.log("userpwd:" + userpwd);
    if(userpwd.trim()==""){
        document.querySelector(".password").placeholder="请输入密码";
        return false;
    }else if( !/^[0-9A-Za-z]{8,128}$/.test(userpwd) ){
        document.querySelector(".password").value=null;
        document.querySelector(".password").placeholder = "密码必须字母数字组成，在8-20个字符之间";
        return false;
    }
    return true
}


function checkRepassword(){
    var repassword = document.querySelector(".repassword").value;
    var  password  = document.querySelector(".password").value;
    if(repassword != password){
        document.querySelector(".repassword").value=null;
        document.querySelector(".repassword").placeholder = "两次密码不一致"
        return false;
    }else{
        return true;
    }
}

function checkUsername(){
    var username = document.querySelector(".username").value;
    console.log("username:" + username);

    if(username.trim() ==""){
        document.querySelector(".username").placeholder = "用户名必须填写";
        return false;
    }else if( !/^[0-9A-Za-z]{6,20}$/.test(username) ){
        document.querySelector(".username").value = null;
        document.querySelector(".username").placeholder = "用户名必须字母数字组成，在6-20个字符之间";
        return false;
    }
        return true;

}

// 提交表单 再验证一次
document.querySelector(".login").onsubmit = function(){
    return checkUsername() && checkPassword() && checkRepassword();
}

//获得表单中的数据
document.querySelector(".submit").onclick=function (){
    var telephone =document.querySelector(".telephone").value;
    var userpwd = document.querySelector(".password").value;
    var username = document.querySelector(".username").value;
    console.log("phone:"+telephone+",userpwd:"+userpwd+",username:"+username);
}
